#ifndef _CTABBOX_H_
#define _CTABBOX_H_

#include <buola/widgets/cwidget.h>

#include <buola/utility/uevent.h>

namespace buola { namespace gui {

class CTabBoxItem
{
public:
    int mID;
    std::wstring mText;
    PIcon mIcon;

    PWindow mCanvas;

    CPoint_d mPos;
    CSize_d mSize;

    bool mPoints;
    int mVisibleText;
};

class CTabBox : public CWidget
{
public:
    CTabBox();
    ~CTabBox();

protected:
    int GetItemPos(int);
    void SelectItemPos(int);

public:
    CTabBoxItem *AddItem(const std::wstring&,PIcon=nullptr,PWindow pCanvas=nullptr,int=-1);
    bool RemoveItem(int);
    void RemoveAll();
    std::wstring GetItemText(int);
    void SetItemText(int,const std::wstring&);

    CTabBoxItem *FindItem(const std::wstring&);
    CTabBoxItem *GetItem(int);

    void SelectItem(int);
    int GetSelItem();

    void SetIconSize(int);
    int GetIconSize()   {   return mIconSize;   }

public:
////////////// Event handling //////////////
    virtual void OnPreExpose();
    virtual void OnExpose(const CExposeEvent &pE);
    virtual void OnButtonDown(const CMouseEvent &pE);
    virtual void OnButtonUp(const CMouseEvent &pE);
    virtual void OnMouseMove(const CMouseEvent &pE);
    virtual void OnSize(const CGeometryEvent &pE);

    virtual void OnStyleChanged(TStyle);
    void OnCtrlTab();

protected:
/////////////// protected functions //////////////
    void ShapeItem(int);
    void AdjustItem(int,double pWidth);

    bool HitTestItem(int,const CPoint_d&);

public:
    UEvent<void(CTabBoxItem*,TID)> eActivated;
    UEvent<void(CTabBoxItem*,TID)> eCloseItem;

private:
    int mNextID;

    int mSelPos;

    CSize_d mTextSize;

//    int mNumRows;

    std::vector<CTabBoxItem*> mItems;

    int mIconSize;

    img::PFont mFont;

    CRect_d mCanvasRect;

    std::unique_ptr<CAccelerator> mCtrlTabAccel;

    bool mIsPosValid;

    int mFirstVisible,mLastVisible;
    bool mHidden;

/////////////////// static data ////////////////////
};

/*namespace gui*/ } /*namespace buola*/ }

#endif
